//
//  GizManager.m
//  PhnixHome
//
//  Created by Levy Xu on 2018/3/3.
//  Copyright © 2018年 Levy Xu. All rights reserved.
//

#import "GizManager.h"

#define kConnectTime 60

@interface GizManager()<GizWifiSDKDelegate>
@property(nonatomic, copy) WifiFinishBlock finishBlock;
@end

@implementation GizManager

SingletonM(GizManager, [self initMethod];)

-(void)initMethod {
    [GizWifiSDK setLogLevel:GizLogPrintNone];
    
    [GizWifiSDK sharedInstance].delegate = self;
    
    NSDictionary *appInfo = @{ @"appId": kGizAppID, @"appSecret": kGizAppSecret };
    NSDictionary *product1 = @{ @"productKey": kProductKeyAP736R, @"productSecret": kProductSecretAP736R };
    NSDictionary *product2 = @{ @"productKey": kProductKeyA725R, @"productSecret": kProductSecretA725R };
    NSDictionary *cloundInfo = @{ @"openAPIInfo": DEFAULT_API_DOMAIN, @"siteInfo": DEFAULT_SITE_DOMAIN };
    
    [GizWifiSDK startWithAppInfo:appInfo productInfo:@[ product1, product2 ] cloudServiceInfo:cloundInfo autoSetDeviceDomain:NO];
}

- (void)initToken {
    WS(ws);
    [GizToken loginAnonymous:^(GizToken *token) {
        if (token) {
            ws.token = token;
            
            [[GizWifiSDK sharedInstance] getBoundDevices:token.uid token:token.token];
        }
    }];
}



#pragma mark - Wifi Connect
-(void)wifiConnect:(NSString *)ssid key:(NSString *)key finishBlock:(WifiFinishBlock)finishBlock {
    if (_isConnecting) {
        [self stopWifiConnect];
    }
    _isConnecting = YES;
    _finishBlock = finishBlock;
    
    [[GizWifiSDK sharedInstance] setDeviceOnboarding:ssid key:key configMode:GizWifiAirLink softAPSSIDPrefix:nil timeout:kConnectTime wifiGAgentType:@[@(GizGAgentHF)]];
}

-(void)stopWifiConnect {
    [[GizWifiSDK sharedInstance] stopDeviceOnboarding];
    _isConnecting = NO;
}

#pragma mark - GizWifiSDKDelegate
-(void)wifiSDK:(GizWifiSDK *)wifiSDK didSetDeviceOnboarding:(NSError *)result device:(GizWifiDevice *)device {
    LOG(@"device %@ result %@",device.mj_keyValues, result.description);
    
    [self stopWifiConnect];
    
    if(result.code == GIZ_SDK_SUCCESS) {
        !self.finishBlock ?: self.finishBlock(device);
    } else {
        !self.finishBlock ?: self.finishBlock(nil);
    }
}

-(void)wifiSDK:(GizWifiSDK *)wifiSDK didUnbindDevice:(NSError *)result did:(NSString *)did {
    LOG(@"did %@",did);
    if(result.code == GIZ_SDK_SUCCESS) {
        [[GizWifiSDK sharedInstance] getBoundDevices:_token.uid token:_token.token];
    } else {
        
    }
}

- (void)wifiSDK:(GizWifiSDK * _Nonnull)wifiSDK didDiscovered:(NSError * _Nonnull)result deviceList:(NSArray <GizWifiDevice *>* _Nullable)deviceList {
    for (GizWifiDevice *device in deviceList) {
        LOG(@"device %@",device.mj_keyValues);
        if(!device.isSubscribed) {
            [device subscribe:YES];
        }
    }
    [NNCDC postNotificationName:kGizDeviceListChangedNotification object:nil];
}

- (void)wifiSDK:(GizWifiSDK *)wifiSDK didNotifyEvent:(GizEventType)eventType eventSource:(id)eventSource eventID:(GizWifiErrorCode)eventID eventMessage:(NSString *)eventMessage {
    if(eventType == GizEventSDK) {
        // SDK发生异常的通知
        LOG(@"SDK event happened: [%@] = %@", @(eventID), eventMessage);
    } else if(eventType == GizEventDevice) {
        // 设备连接断开时可能产生的通知
        GizWifiDevice* mDevice = (GizWifiDevice*)eventSource;
        LOG(@"device mac %@ disconnect caused by %@", mDevice.macAddress, eventMessage);
    } else if(eventType == GizEventM2MService) {
        // M2M服务返回的异常通知
        LOG(@"M2M domain %@ exception happened: [%@] = %@", (NSString*)eventSource, @(eventID), eventMessage);
    } else if(eventType == GizEventToken) {
        // token失效通知
        LOG(@"token %@ expired: %@", (NSString*)eventSource, eventMessage);
        
        [self initToken];
    }
}
@end
